//
//  WBStatusCell.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/17.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit
import SDWebImage
import YYText
private let margin:CGFloat = 8
//配图视图之间的间距
private let imageMargin: CGFloat = 2
private let maxWidth = ScreenWidth - 2 * margin
private let itemWidth = (maxWidth - 2 * imageMargin) / 3
class WBStatusCell: UITableViewCell {
    @IBOutlet weak var contentLabel: YYLabel!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var icon: UIImageView!
    @IBOutlet weak var Img_avastar: UIImageView!
    @IBOutlet weak var tiemLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var Img_mbRank: UIImageView!
    @IBOutlet weak var pictureView: WBPictureView!
    //布局约束
    @IBOutlet weak var picHeight: NSLayoutConstraint!
    @IBOutlet weak var picWidth: NSLayoutConstraint!
    @IBOutlet weak var picFlowLayout: UICollectionViewFlowLayout!
    @IBOutlet weak var toolBar: UIView!
    @IBOutlet weak var retweetLabel: UILabel!
    @IBOutlet weak var pictureTop: NSLayoutConstraint!
    //cell需要绑定数据
        var viewModel: WBStatusViewModel?{
            didSet{
                //设置子控件数据
                icon.sd_setImage(with: viewModel?.iconURL)
                tiemLabel.text = viewModel?.timeText
                sourceLabel.text = viewModel?.sourceText
                contentLabel.attributedText = viewModel?.originalAttributedString
                nameLabel.text = viewModel?.status?.user?.name
                Img_mbRank.image = viewModel?.mbRankImage
                Img_avastar.image = viewModel?.verifitedImage
                
                //修改count的获取方式
                let count = viewModel?.imageURLs?.count ?? 0
                //根据count值 修改顶部约束对象的值
                pictureTop.constant = (count == 0 ? 0 : margin)
                
                let result = calculatePictureViewSize(count: count)
                //根据约束对象的约束值
                picWidth.constant = result.pSize.width
                picHeight.constant = result.pSize.height
                //还需要更新itemSize 会出现复用的问题
                picFlowLayout.itemSize = result.itemSize

                //给配图视图设置数据源
                //转发和原创微博都是公用了配图视图
                pictureView.pictureInfos = viewModel?.imageURLs

                
                //8.1  不会立即更行约束,配图视图的大小还是使用的上一个cell的尺寸
                //设置配图视图的文本
                retweetLabel?.text = viewModel?.status?.retweeted_status?.text
                pictureView.layoutIfNeeded()
                //实现点击高亮的闭包
                //void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)
                contentLabel.highlightTapAction = { (containerView,text,range,rect) -> () in
                    //containerView,就是被点击的视图
                    //text: 被点击的属性文本
                    //range: 点击的特殊文本 范围
                    //rect: 点击的特殊的文本的坐标
                    let sub = (text.string as NSString).substring(with: range)
                    print(sub)
                    //跳转页面   响应者链条来实现页面跳转
                    //当用户点击的是url的时候 就执行跳转页面 显示对应的网页
                     //判断如果是 http开头的就认为是网页
                    if sub.hasPrefix("http") {
                        //实力化控制器
                        let web = WBWebViewController()
                        //push  在cell的响应者链条中获取导航视图控制器
                        //设置urlString
                        web.urlString = sub
                        self.findNavController()?.pushViewController(web, animated: true)
                    }
                }


            }
        
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置背景色（随机色）
//        self.contentView.backgroundColor = UIColor.white
        //设置行数为 0
        self.contentLabel.numberOfLines = 0
        //设置最大的布局宽度
        self.contentLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        self.retweetLabel?.numberOfLines = 0
        self.retweetLabel?.preferredMaxLayoutWidth = ScreenWidth - 2 * margin
        //设置选中的样式
        selectionStyle = .none
        //设置流水布局
        picFlowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        //设置行和列最小间距
        picFlowLayout.minimumLineSpacing = imageMargin
        picFlowLayout.minimumInteritemSpacing = imageMargin
    }
    //根据图片的张数计算配图视图的大小,更新配图视图的宽高约束对象的值
        private func calculatePictureViewSize(count: Int) -> (pSize: CGSize,itemSize: CGSize) {
            if count == 0 {
                return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
            }
            
            if count == 1 {
                //计算图片的大小  获取图片  -> image.size  必须确保在这里一定能够获取到图片
                //不能够在这个地方下载图片 否则会执行多次刷新(有多少个单张图片就需要刷新多少次)
                if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.imageURLs?.last?.wap360 ?? "") {
                    let imageSize = image.size
                    //不能再if中单独设置itemSize
                    //pictureViewFlowLayout.itemSize = imageSize
                    return (imageSize,imageSize)
                }
                
                return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))
            }
            
            //判断四张图片
            if count == 4 {
                let width = 2 * itemWidth + margin
                return (CGSize(width: width, height: width),CGSize(width: itemWidth, height: itemWidth))
            }
        //其他的多张图片 需要确定行数
        /*
         1,2,3  1
         4,5,6  2
         7,8,9  3
         */
            //一只列数和总个数 需要确定行数
            let row = CGFloat((count - 1) / 3) + 1
            let h = row * itemWidth + (row - 1) * imageMargin
            return (CGSize(width: maxWidth, height: h),CGSize(width: itemWidth, height: itemWidth))
     }

  }

